package com.sq.auth.application.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sq.auth.application.dto.AuthRoleDTO;
import com.sq.auth.application.dto.AuthUserDTO;
import com.sq.auth.common.entity.Result;
import com.sq.auth.domain.bo.AuthRoleBO;
import com.sq.auth.domain.bo.AuthUserBO;
import com.sq.auth.domain.service.AuthRoleDomainService;
import com.sq.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author quan
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class AuthRoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;

    @PostMapping("add")
    public Result add(@RequestBody AuthRoleDTO authRoleDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
        }
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
            Preconditions.checkArgument(StrUtil.isNotBlank(authRoleDTO.getRoleKey()), "角色标识不能为空");
            AuthRoleBO authRoleBO =  BeanUtil.copyProperties(authRoleDTO, AuthRoleBO.class);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("AuthRoleController.add.error:{}", e.getMessage());
            return Result.fail("添加角色失败");
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody AuthRoleDTO authRoleDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
        }
        try {
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO =  BeanUtil.copyProperties(authRoleDTO, AuthRoleBO.class);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("AuthRoleController.update.error:{}", e.getMessage());
            return Result.fail("更新角色失败");
        }
    }

    @PostMapping("delete")
    public Result delete(@RequestBody AuthRoleDTO authRoleDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
        }
        try {
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO =  BeanUtil.copyProperties(authRoleDTO, AuthRoleBO.class);
            return Result.ok(authRoleDomainService.delete(authRoleBO));
        } catch (Exception e) {
            log.error("AuthRoleController.delete.error:{}", e.getMessage());
            return Result.fail("删除角色失败");
        }
    }
}
